import 'package:demo/mixin/view_state_type_change_mixin.dart';
import 'package:demo/provider/view_model/base_view_model.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart' show ChangeNotifierProvider, Selector;

export 'package:provider/provider.dart';

/// 回调创建函数
typedef void OnModelReady<T>(T viewModel);

/// 基于ChangeNotifierProvider进行封装
class BaseProviderWidget<T extends BaseViewModel> extends StatefulWidget {
  /// 对应的provider监听
  final T viewModel;

  /// 对应的子类
  final Widget child;

  /// 空数据布局
  final Widget? emptyChild;

  /// loading布局
  final Widget? loadingChild;

  /// 错误布局
  final Widget? errChild;

  /// provider的过渡
  final TransitionBuilder? transitionBuilder;

  /// 回调函数
  final OnModelReady<T>? onModelReady;

  /// 是否现在在非idle的情况下的header头部
  final bool showNoIdleHeader;

  /// 空数据情况的参数提示
  final String emptyDataTips;

  /// 错误数据情况的参数提示
  final String errDataTips;

  BaseProviderWidget({
    Key? key,
    required this.viewModel,
    required this.child,
    this.emptyChild,
    this.loadingChild,
    this.errChild,
    this.onModelReady,
    this.transitionBuilder,
    this.showNoIdleHeader = true,
    this.emptyDataTips = "暂无更多数据",
    this.errDataTips = "加载失败...",
  }) : super(key: key);

  _BaseProviderWidgetState<T> createState() => _BaseProviderWidgetState<T>();
}

class _BaseProviderWidgetState<S extends BaseViewModel> extends State<BaseProviderWidget<S>> {
  @override
  void initState() {
    super.initState();
    widget.onModelReady?.call(widget.viewModel);
  }

  /// 加载loading的控件
  Widget get _buildLoadingStateWidget {
    return Scaffold(
      appBar: widget.showNoIdleHeader ? AppBar() : null,
      body: Center(
        child: Container(
          alignment: Alignment.center,
          height: 88,
          width: 88,
          decoration: BoxDecoration(
            color: Colors.white,
            boxShadow: const [
              BoxShadow(
                offset: const Offset(0, 2),
                blurRadius: 3,
                spreadRadius: 0,
                color: Color(0x0F000000),
              ),
            ],
            borderRadius: BorderRadius.circular(8),
          ),
          child: const CircularProgressIndicator(),
        ),
      ),
    );
  }

  /// 加载loading的控件
  Widget get _buildEmptyDataStateWidget {
    return Scaffold(
      appBar: widget.showNoIdleHeader ? AppBar() : null,
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Image.asset("assets/images/common/empty_data.png", width: 165, height: 165),
            Text(
              widget.emptyDataTips,
              style: const TextStyle(fontSize: 12, color: Color(0xFF707070)),
            ),
          ],
        ),
      ),
    );
  }

  /// 发生错误的控件
  Widget get _buildErrorStateWidget {
    return Scaffold(
      appBar: widget.showNoIdleHeader ? AppBar() : null,
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Image.asset("assets/images/common/error_data.png", width: 165, height: 165),
            Text(
              widget.errDataTips,
              style: const TextStyle(fontSize: 12, color: Color(0xFF707070)),
            ),
            GestureDetector(
              onTap: () => widget.onModelReady!.call(widget.viewModel),
              child: Container(
                margin: const EdgeInsets.only(top: 12),
                height: 43,
                width: 192,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(2),
                  color: const Color(0xFF3291FA),
                ),
                alignment: Alignment.center,
                child: const Text(
                  "点击刷新",
                  style: const TextStyle(fontSize: 14, color: Colors.white, fontWeight: FontWeight.w500),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<S>.value(
      value: widget.viewModel,
      child: Selector<S, ViewState>(
        builder: (_, ViewState value, Widget? child) {
          switch (value) {
            case ViewState.Loading:
              return widget.loadingChild ?? _buildLoadingStateWidget;

            case ViewState.Error:
              return widget.errChild ?? _buildErrorStateWidget;

            case ViewState.Empty:
              return widget.emptyChild ?? _buildEmptyDataStateWidget;

            case ViewState.Idle:
            default:
              return widget.child;
          }
        },
        selector: (_, S model) => model.viewState,
      ),
      builder: widget.transitionBuilder,
    );
  }
}
